package services

import (
	"gitee.com/zhengjianfeng/pay/common"
	"gitee.com/zhengjianfeng/pay/dao"
	"gitee.com/zhengjianfeng/pay/datasource"
	"gitee.com/zhengjianfeng/pay/models"
	"time"
)

type PayAgentService interface {
	CreateAgent(agentName string, agentRate int, parentId int64) (AgentId int64, err error)
	GetAgentRate(agentId int64) (agentRate int, err error)
}

type payAgentService struct {
	payAgentDao *dao.PayAgentDao
}

func NewPayAgentService() *payAgentService {
	return &payAgentService{
		payAgentDao: dao.NewPayAgentDao(datasource.InstanceMaster()),
	}
}

func (s *payAgentService) CreateAgent(agentName string, agentRate int, parentId int64) (AgentId int64, err error) {
	var (
		agentInfo *models.PayAgent
		now       time.Time
	)

	now = time.Now()

	agentInfo = &models.PayAgent{
		AgentName: agentName,
		AgentRate: agentRate,
		Status:    common.PAY_ACCOUNT_STATUS_OPEN,
		ParentId:  parentId,
		GmtModify: now,
		GmtCreate: now,
	}

	err = s.payAgentDao.GetSave(agentInfo)

	AgentId = agentInfo.Id

	return
}

func (s *payAgentService) GetAgentRate(agentId int64) (agentRate int, err error) {
	agentRate, err = s.payAgentDao.GetAgentRate(agentId)
	return
}
